home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / pascal / oop55.zip / COUNT.PAS < prev    next >
Pascal/Delphi Source File  |  1989-05-02  |  5KB  |  239 lines

  1.  
  2. { Copyright (c) 1989 by Borland International, Inc. }
  3.  
  4. unit Count;
  5. { Turbo Pascal 5.5 object-oriented example.
  6.   See BREAKOUT.PAS.
  7.   This unit provides several text display object types, most of
  8.   which are coupled with various types of counters.
  9. }
  10.  
  11. interface
  12.  
  13. uses Screen;
  14.  
  15. const
  16.   StrSize = 40;
  17.  
  18. type
  19.   TextStr = String[StrSize];
  20.   TextPtr = ^TextStr;
  21.  
  22.   TextString = object(Location)
  23.     Text : TextPtr;
  24.     Attr : Byte;
  25.     constructor Init(InitX, InitY : Integer;
  26.                      InitText : TextStr;
  27.                      InitAttr : Byte);
  28.     procedure Show; virtual;
  29.     procedure Hide; virtual;
  30.   end;
  31.  
  32.   Counter = object(TextString)
  33.     Value : Integer;
  34.     BaseValue : Integer;
  35.     constructor Init(InitValue, InitX, InitY : Integer;
  36.                      InitName : TextStr;
  37.                      InitAttr : Byte);
  38.     procedure Show; virtual;
  39.     procedure Hide; virtual;
  40.     procedure ShowVal; virtual;
  41.     procedure HideVal; virtual;
  42.     procedure SetValue(NewValue : Integer);
  43.     procedure Reset;
  44.     procedure Increment;
  45.     procedure Decrement;
  46.     procedure Add(Incr : Integer);
  47.     function Equal(TestValue : Integer) : Boolean;
  48.     function GetValue : Integer;
  49.   end;
  50.  
  51.   DownCounter = object(Counter)
  52.     Minimum : Integer;
  53.     constructor Init(InitValue, InitX, InitY, InitMin : Integer;
  54.                      InitName : TextStr;
  55.                      InitAttr : Byte);
  56.     procedure Decrement;
  57.     procedure Add(Incr : Integer);
  58.     function Last : Boolean;
  59.   end;
  60.  
  61.   LimitCounter = object(DownCounter)
  62.     Maximum : Integer;
  63.     constructor Init(InitValue, InitX, InitY, InitMin, InitMax : Integer;
  64.                      InitName : TextStr;
  65.                      InitAttr : Byte);
  66.     procedure Increment;
  67.     procedure Add(Incr : Integer);
  68.   end;
  69.  
  70. implementation
  71.  
  72. uses Crt;
  73.  
  74. constructor TextString.Init(InitX, InitY : Integer;
  75.                             InitText : TextStr;
  76.                             InitAttr : Byte);
  77. begin
  78.   Location.Init(InitX, InitY);
  79.   Attr := InitAttr;
  80.   GetMem(Text, Length(InitText) + 1);
  81.   Move(InitText, Text^, Length(InitText) + 1);
  82. end;
  83.  
  84. procedure TextString.Show;
  85. begin
  86.   Visible := True;
  87.   GoToXY(X, Y);
  88.   TextColor(Attr);
  89.   Write(Text^);
  90. end;
  91.  
  92. procedure TextString.Hide;
  93. begin
  94.   Visible := False;
  95.   GoToXY(X, Y);
  96.   TextAttr := Attr;
  97.   Write('' : Length(Text^));
  98. end;
  99.  
  100. constructor Counter.Init(InitValue, InitX, InitY : Integer;
  101.                          InitName : TextStr;
  102.                          InitAttr : Byte);
  103. begin
  104.   TextString.Init(InitX, InitY, InitName, InitAttr);
  105.   BaseValue := InitValue;
  106.   Value := InitValue;
  107. end;
  108.  
  109. procedure Counter.Show;
  110. begin
  111.   Visible := True;
  112.   GoToXY(X, Y);
  113.   TextColor(Attr);
  114.   Write(Text^, ': ', Value);
  115. end;
  116.  
  117. procedure Counter.Hide;
  118. begin
  119.   Visible := False;
  120.   GoToXY(X, Y);
  121.   TextAttr := Attr;
  122.   Write('' : Length(Text^) + 7);
  123. end;
  124.  
  125. procedure Counter.ShowVal;
  126. begin
  127.   Visible := True;
  128.   GoToXY(X + Length(Text^) + 2, Y);
  129.   TextColor(Attr);
  130.   Write(Value);
  131. end;
  132.  
  133. procedure Counter.HideVal;
  134. begin
  135.   Visible := False;
  136.   GoToXY(X + Length(Text^) + 2, Y);
  137.   TextAttr := Attr;
  138.   Write('' : 5);
  139. end;
  140.  
  141. procedure Counter.SetValue(NewValue : Integer);
  142. var
  143.   Vis : Boolean;
  144. begin
  145.   Vis := Visible;
  146.   if Vis then
  147.     HideVal;
  148.   Value := NewValue;
  149.   if Vis then
  150.     ShowVal;
  151. end;
  152.  
  153. procedure Counter.Increment;
  154. begin
  155.   SetValue(Value + 1);
  156. end;
  157.  
  158. procedure Counter.Decrement;
  159. begin
  160.   SetValue(Value - 1);
  161. end;
  162.  
  163. procedure Counter.Add(Incr : Integer);
  164. begin
  165.   SetValue(Value + Incr);
  166. end;
  167.  
  168. procedure Counter.Reset;
  169. begin
  170.   SetValue(BaseValue);
  171. end;
  172.  
  173. function Counter.Equal(TestValue : Integer) : Boolean;
  174. begin
  175.   Equal := (Value = TestValue);
  176. end;
  177.  
  178. function Counter.GetValue : Integer;
  179. begin
  180.   GetValue := Value;
  181. end;
  182.  
  183. constructor DownCounter.Init(InitValue, InitX, InitY, InitMin : Integer;
  184.                              InitName : TextStr;
  185.                              InitAttr : Byte);
  186. begin
  187.   Counter.Init(InitValue, InitX, InitY, InitName, InitAttr);
  188.   Minimum := InitMin;
  189. end;
  190.  
  191. procedure DownCounter.Decrement;
  192. begin
  193.   if Value > Minimum then
  194.     Counter.Decrement;
  195. end;
  196.  
  197. procedure DownCounter.Add(Incr : Integer);
  198. var
  199.   Temp : Integer;
  200. begin
  201.   Temp := GetValue + Incr;
  202.   if Temp >= Minimum then
  203.     SetValue(Temp);
  204. end;
  205.  
  206. function DownCounter.Last : Boolean;
  207. begin
  208.   Last := (Value = Minimum);
  209. end;
  210.  
  211. constructor LimitCounter.Init(InitValue,
  212.                               InitX,
  213.                               InitY,
  214.                               InitMin,
  215.                               InitMax : Integer;
  216.                               InitName : TextStr;
  217.                               InitAttr : Byte);
  218. begin
  219.   DownCounter.Init(InitValue, InitX, InitY, InitMin, InitName, InitAttr);
  220.   Maximum := InitMax;
  221. end;
  222.  
  223. procedure LimitCounter.Increment;
  224. begin
  225.   if Value < Maximum then
  226.     Counter.Increment;
  227. end;
  228.  
  229. procedure LimitCounter.Add(Incr : Integer);
  230. var
  231.   Temp : Integer;
  232. begin
  233.   Temp := Value + Incr;
  234.   if (Temp <= Maximum) and (Temp >= Minimum) then
  235.     SetValue(Temp);
  236. end;
  237.  
  238. end.
  239.